import Vue from 'vue'
import VueRouter from 'vue-router'
import login from './login'
import admin from './admin'

Vue.use(VueRouter)

const routes = [
  login,
  admin,
  {
    path: '*',
    redirect: 'login'
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from ,next)=>{// 路由拦截器，在router中的引用方法
  if(!localStorage.getItem('id') && to.meta.istoken){
    next('/login')
    Vue.prototype.$alert('请先登录!', '提示', {
      confirmButtonText: '确定'
    });
    return;
  }
  next();
})

export default router
